import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:signage/app/modules/device/views/rotate_image_view.dart';
import 'package:signage/l10n/l10n.dart';
import 'package:signage/widgets/custom_appbar.dart';
import 'package:signage/widgets/device_list_item.dart';
import '../controllers/add_device_controller.dart';
import '../../../../utils/dialog_utils.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class AddDeviceView extends GetView<AddDeviceController> {
  final String? source;
  const AddDeviceView({
    super.key,
    this.source,
  });
  @override
  Widget build(BuildContext context) {
    return Obx(() =>
        controller.type == 'nfc'
            ? Column(
                children: [
                  24.verticalSpace,
            Text(
              textAlign: TextAlign.center,
              controller.bluetoothManager.nfcScanResults
                                  .where((element) =>
                                      controller.bluetoothManager
                                          .deviceInfoMap[element.id] ==
                                      null)
                                  .isEmpty
                  ? '请将设备靠近NFC感应区'
                  : '扫描设备',
              style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.w400,
                letterSpacing: 0,
                height: 1.35,
                color: Color(0XFF333333),
              ),
            ),
            Expanded(
              child: Stack(
                children: [
                  // 扫描动画
                  Obx(() => controller.bluetoothManager.nfcScanResults
                                  .where((element) =>
                                      controller.bluetoothManager
                                          .deviceInfoMap[element.id] ==
                                      null)
                                  .isEmpty
                      ? Center(
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              RotateImageView(),
                              const SizedBox(height: 10),
                              const Text(
                                'NFC设备扫描中',
                                style: TextStyle(
                                    fontSize: 17, color: Color(0XFF333333)),
                              ),
                            ],
                          ),
                        )
                      : const SizedBox(height: 20)),
                  // 设备列表
                  controller.bluetoothManager.nfcScanResults
                                  .where((element) =>
                                      controller.bluetoothManager
                                          .deviceInfoMap[element.id] ==
                                      null)
                                  .isNotEmpty
                      ? Obx(
                          () => (controller.bluetoothManager.nfcScanResults
                                  .where((element) =>
                                      controller.bluetoothManager
                                          .deviceInfoMap[element.id] ==
                                      null)
                                  .isEmpty)
                              ? Center(
                                  child: Column(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: [
                                      Image.asset(
                                        'assets/images/Group 1321318735@2x.png',
                                        width: 232.w,
                                        height: 200.h,
                                      ),
                                      const SizedBox(height: 16),
                                      Text(
                                        '暂无设备',
                                        style: TextStyle(
                                          fontSize: 15.sp,
                                          color: Colors.black54,
                                        ),
                                      ),
                                    ],
                                  ),
                                )
                              : Container(
                                  margin: EdgeInsets.all(16.w),
                                  padding: EdgeInsets.all(16.w),
                                  decoration: BoxDecoration(
                                    color: Colors.white,
                                    borderRadius: BorderRadius.circular(12.r),
                                  ),
                                  child: Column(
                                    children: [
                                      Row(
                                        children: [
                                          Text(
                                            '点击需要绑定的设备',
                                            style: TextStyle(
                                              fontSize: 14.sp,
                                              fontWeight: FontWeight.w400,
                                              letterSpacing: 0,
                                              color: Color(0XFF333333),
                                            ),
                                          ),
                                        ],
                                      ),
                                      15.verticalSpace,
                                      Expanded(
                                          child: ListView.builder(
                                        padding: EdgeInsets.zero,
                                          itemCount: controller.bluetoothManager
                                              .nfcScanResults
                                              .where((element) =>
                                                  controller
                                                          .bluetoothManager
                                                          .deviceInfoMap[
                                                              element.id] ==
                                                      null)
                                              .length,
                                        itemBuilder: (context, index) {
                                          return _buildDeviceListItem(
                                              context, index);
                                        },
                                      )),
                                      Row(
                                        children: [
                                          SizedBox(
                                            width: 18.w,
                                            child: Checkbox(
                                              checkColor: Colors.black,
                                              value: controller
                                                      .selectedDevices.length ==
                                                  controller.bluetoothManager
                                                      .nfcScanResults.length,
                                              shape: RoundedRectangleBorder(
                                                  borderRadius:
                                                      BorderRadius.circular(
                                                          50.r)),
                                              onChanged: (value) {
                                                if (value != null) {
                                                  if (value) {
                                                    controller
                                                        .selectedAllDevices();
                                                  } else {
                                                    controller.selectedDevices
                                                        .value = [];
                                                  }
                                                }
                                              },
                                            ),
                                          ),
                                          10.horizontalSpace,
                                          Text(
                                            '全选',
                                            style: TextStyle(
                                              fontSize: 16.sp,
                                              fontWeight: FontWeight.w400,
                                              letterSpacing: 0,
                                              color: Color(0XFF333333),
                                            ),
                                          ),
                                          40.horizontalSpace,
                                          Expanded(
                                            child: GestureDetector(
                                              onTap: () {
                                                controller.bindAllDevices();
                                              },
                                              child: Container(
                                                alignment: Alignment.center,
                                                height: 40.h,
                                                decoration: BoxDecoration(
                                                  color: const Color.fromRGBO(
                                                      252, 207, 1, 1),
                                                  borderRadius:
                                                      BorderRadius.circular(
                                                          28.r),
                                                ),
                                                child: Text(
                                                  '绑定',
                                                  style: TextStyle(
                                                    color: Color(0XFF333333),
                                                    fontSize: 16.sp,
                                                    fontWeight: FontWeight.w400,
                                                    letterSpacing: 0,
                                                  ),
                                                ),
                                              ),
                                            ),
                                          ),
                                        ],
                                      )
                                    ],
                                  ),
                                ),
                        )
                      : const SizedBox.shrink(),
                ],
              ),
            ),
          ],
        )
        :  Column(
                children: [
                  24.verticalSpace,
            Text(
              textAlign: TextAlign.center,
              controller.bluetoothManager.connectedDevices
                                  .where((element) =>
                                      controller.bluetoothManager
                                          .deviceInfoMap[element.id] ==
                                      null)
                                  .isEmpty
                  ? AppLocalizations.of(Get.context!)!.translate('device_scanning_bluetooth')
                  : AppLocalizations.of(Get.context!)!.translate('scan_device_list'),
              style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.w400,
                letterSpacing: 0,
                height: 1.35,
                color: Color(0XFF333333),
              ),
            ),
            Expanded(
              child: Stack(
                children: [
                  // 扫描动画
                  Obx(() => controller.bluetoothManager.connectedDevices
                                  .where((element) =>
                                      controller.bluetoothManager
                                          .deviceInfoMap[element.id] ==
                                      null)
                                  .isEmpty
                      ? Center(
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              RotateImageView(),
                              const SizedBox(height: 10),
                              Text(
                                AppLocalizations.of(Get.context!)!
                                    .translate('device_scanning_bluetooth'),
                                style: TextStyle(
                                    fontSize: 17, color: Color(0XFF333333)),
                              ),
                            ],
                          ),
                        )
                      : const SizedBox(height: 20)),
                  // 设备列表
                  controller.bluetoothManager.connectedDevices
                                  .where((element) =>
                                      controller.bluetoothManager
                                          .deviceInfoMap[element.id] ==
                                      null)
                                  .isNotEmpty
                      ? Obx(
                          () => (controller.bluetoothManager.connectedDevices
                                  .where((element) =>
                                      controller.bluetoothManager
                                          .deviceInfoMap[element.id] ==
                                      null)
                                  .isEmpty)
                              ? Center(
                                  child: Column(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: [
                                      Image.asset(
                                        'assets/images/Group 1321318735@2x.png',
                                        width: 232.w,
                                        height: 200.h,
                                      ),
                                      const SizedBox(height: 16),
                                      Text(
                                        '暂无设备',
                                        style: TextStyle(
                                          fontSize: 15.sp,
                                          color: Colors.black54,
                                        ),
                                      ),
                                    ],
                                  ),
                                )
                              : Container(
                                  margin: EdgeInsets.all(16.w),
                                  padding: EdgeInsets.all(16.w),
                                  decoration: BoxDecoration(
                                    color: Colors.white,
                                    borderRadius: BorderRadius.circular(12.r),
                                  ),
                                  child: Column(
                                    children: [
                                      Row(
                                        children: [
                                          Text(
                                            '点击需要绑定的设备',
                                            style: TextStyle(
                                              fontSize: 14.sp,
                                              fontWeight: FontWeight.w400,
                                              letterSpacing: 0,
                                              color: Color(0XFF333333),
                                            ),
                                          ),
                                        ],
                                      ),
                                      15.verticalSpace,
                                      Expanded(
                                          child: ListView.builder(
                                        padding: EdgeInsets.zero,
                                          itemCount: controller.bluetoothManager
                                              .connectedDevices
                                              .where((element) =>
                                                  controller
                                                          .bluetoothManager
                                                          .deviceInfoMap[
                                                              element.id] ==
                                                      null)
                                              .length,
                                        itemBuilder: (context, index) {
                                          return _buildDeviceListItem(
                                              context, index);
                                        },
                                      )),
                                      Row(
                                        children: [
                                          SizedBox(
                                            width: 18.w,
                                            child: Checkbox(
                                              checkColor: Colors.black,
                                              value: controller
                                                      .selectedDevices.length ==
                                                  controller.bluetoothManager
                                                      .connectedDevices.length,
                                              shape: RoundedRectangleBorder(
                                                  borderRadius:
                                                      BorderRadius.circular(
                                                          50.r)),
                                              onChanged: (value) {
                                                if (value != null) {
                                                  if (value) {
                                                    controller
                                                        .selectedAllDevices();
                                                  } else {
                                                    controller.selectedDevices
                                                        .value = [];
                                                  }
                                                }
                                              },
                                            ),
                                          ),
                                          10.horizontalSpace,
                                          Text(
                                            '全选',
                                            style: TextStyle(
                                              fontSize: 16.sp,
                                              fontWeight: FontWeight.w400,
                                              letterSpacing: 0,
                                              color: Color(0XFF333333),
                                            ),
                                          ),
                                          40.horizontalSpace,
                                          Expanded(
                                            child: GestureDetector(
                                              onTap: () {
                                                controller.bindAllDevices();
                                              },
                                              child: Container(
                                                alignment: Alignment.center,
                                                height: 40.h,
                                                decoration: BoxDecoration(
                                                  color: const Color.fromRGBO(
                                                      252, 207, 1, 1),
                                                  borderRadius:
                                                      BorderRadius.circular(
                                                          28.r),
                                                ),
                                                child: Text(
                                                  '绑定',
                                                  style: TextStyle(
                                                    color: Color(0XFF333333),
                                                    fontSize: 16.sp,
                                                    fontWeight: FontWeight.w400,
                                                    letterSpacing: 0,
                                                  ),
                                                ),
                                              ),
                                            ),
                                          ),
                                        ],
                                      )
                                    ],
                                  ),
                                ),
                        )
                      : const SizedBox.shrink(),
                ],
              ),
            ),
          ],
        ));
  }

  Widget _buildDeviceListItem(BuildContext context, int index) {
    final deviceInfo = controller.type == 'nfc'
        ? controller.bluetoothManager.nfcScanResults
            .where((element) =>
                controller.bluetoothManager.deviceInfoMap[element.id] == null)
            .toList()[index]
        : controller.bluetoothManager.connectedDevices
            .where((element) =>
                controller.bluetoothManager.deviceInfoMap[element.id] == null)
            .toList()[index];

    return DeviceListItem(
      cachedInfo: deviceInfo,
      showRssi: true,
      isFromAddDevice: true,
      onRemove: (String id) {},
      isView: true,
      toggleSelection: (value) {
        print('value: $value');
        controller.onDeviceSelected(deviceInfo.id);
      },
      isSelected: controller.selectedDevices.contains(deviceInfo.id),
      needToggleSelection: true,
    );
  }
}
